package com.example;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mapper.ProductMapper;
import com.example.mapper.UserMapper;
import com.example.pojo.Product;
import com.example.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class MybatisPlusPage {


    @Autowired
    public UserMapper userMapper;


    @Test
    public void  testpage(){
        // 实体类作为对象
        //默认参数第几页？每页几行？
         Page<User> page = new Page<>(3,2);
         userMapper.selectPage(page,null);
         System.out.println("获取当前页数据"+page.getRecords());
        System.out.println("获取总页数"+page.getPages());
        System.out.println("获取数据总条数"+page.getTotal());
        System.out.println("是否有上一页"+page.hasNext());
        System.out.println("是否有下一页"+page.hasPrevious());
        System.out.println("---");
        System.out.println("返回: 此对象的哈希码值"+page.hashCode());
        System.out.println("当前是第几页"+page.getCurrent());
        System.out.println("每页几条数据"+page.getSize());

    }



    @Test
    public void  testpage01(){
        // 实体类作为对象
        //默认参数第几页？每页几行？
        Page<User> page = new Page<>(2,2);
        userMapper.selectpageVo(page,10);
        System.out.println("page"+page);

    }
     @Autowired
     private ProductMapper productMapper;

    @Test
    public void  testProduct01(){
        //1.小李获取商品价格
        Product productLi = productMapper.selectById(1);
        System.out.println("小李获取的商品价格为：" + productLi.getPrice());

        //2.小王获取商品价格
        Product productWang = productMapper.selectById(1);
        System.out.println("小王获取的商品价格为：" + productWang.getPrice());

        //3.小李修改商品价格+50
        productLi.setPrice(productLi.getPrice()+50);
        productMapper.updateById(productLi);

        int result = productMapper.updateById(productWang);
        if(result == 0){
            //操作失败，重试
            Product productNew = productMapper.selectById(1);
            productNew.setPrice(productNew.getPrice()-30);
            productMapper.updateById(productNew);
        }

        //4.小王修改商品价格-30
        productWang.setPrice(productWang.getPrice()-30);
        productMapper.updateById(productWang);


        //5.老板查询商品价格
        Product productBoss = productMapper.selectById(1);
        System.out.println("老板获取的商品价格为：" + productBoss.getPrice());




    }




}
